package com.bkgy.modular.tbdsjruleedit.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bkgy.modular.alarm.entity.ZoneEntity;
import com.bkgy.modular.tbdsjruleedit.dao.TbDsjRuleEditDao;
import com.bkgy.modular.tbdsjruleedit.entity.JTbDsjRuleEdit;
import com.bkgy.modular.tbdsjruleedit.entity.TbDsjRuleEdit;
import com.bkgy.modular.tbdsjruleedit.service.TbDsjRuleEditService;
import com.bkgy.sys.common.entity.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;


/**
* 简易规则编辑表接口具体实现类
* @author  生成器生成
* @date 2022-07-27 09:54:52
*/
@Service
public class TbDsjRuleEditServiceImpl implements TbDsjRuleEditService {
	
	@Autowired
	private TbDsjRuleEditDao tbDsjRuleEditDao;
	@Resource
	private RestTemplate restTemplate;
	
	/**
     * 添加记录
     * @param data json字符串
     * @param //userId 用户id
     * @param //sname 用户姓名
     */
	public ResultData insertDataTbDsjRuleEdit(String data){
		try{
			JTbDsjRuleEdit jtbDsjRuleEdit = JSON.parseObject(data, JTbDsjRuleEdit.class);
    		TbDsjRuleEdit tbDsjRuleEdit = jtbDsjRuleEdit.getTbDsjRuleEdit();
    		//CodiUtil.newRecord(userId,sname,tbDsjRuleEdit);
            tbDsjRuleEditDao.insertDataTbDsjRuleEdit(tbDsjRuleEdit);
            return ResultData.ResultDataSuccessSelf("添加数据成功", null);
    	}catch(Exception e){
    		e.printStackTrace();
    		return ResultData.ResultDataFaultSelf("添加失败，错误信息为" + e.getMessage(), null); 
    	}
	};
	
	/**
	 * 根据主键删除对象
	 * @param //indocno  对象主键
	 * */
	public ResultData deleteDataTbDsjRuleEditOne(String calc_result_variable){
		try {
            tbDsjRuleEditDao.deleteDataTbDsjRuleEditOne(calc_result_variable);
             return ResultData.ResultDataSuccessSelf("删除数据成功", null);
        } catch (Exception e) {
        	e.printStackTrace();
        	return ResultData.ResultDataFaultSelf("删除失败,失败信息为" + e.getMessage(), null); 
        }
	};
	
	/**
     * 根据主键字符串删除多个对象
     * @param str_id 对象主键字符串
     */
    public ResultData deleteDataTbDsjRuleEditMany(String str_id) {
        try {
        	String sql = "delete tb_dsj_rule_edit where calc_result_variable in(" + str_id +")";
            tbDsjRuleEditDao.deleteDataTbDsjRuleEditMany(sql);
            return ResultData.ResultDataSuccessSelf("删除数据数据成功", null);
        } catch (Exception e) {
        	e.printStackTrace();
        	return ResultData.ResultDataFaultSelf("删除失败,失败信息为" + e.getMessage(), null); 
        }
    }
	
	/**
     * 修改记录
     * @param //userId 用户id
     * @param //sname 用户姓名
     * @param data sysUser 对象实体
     */
	public ResultData updateDataTbDsjRuleEdit(String data){
		try {
			JTbDsjRuleEdit jtbDsjRuleEdit = JSON.parseObject(data,JTbDsjRuleEdit.class);
    		TbDsjRuleEdit tbDsjRuleEdit = jtbDsjRuleEdit.getTbDsjRuleEdit();
        	//CodiUtil.editRecord(userId,sname,tbDsjRuleEdit);
            tbDsjRuleEditDao.updateDataTbDsjRuleEdit(tbDsjRuleEdit);
            return ResultData.ResultDataSuccessSelf("修改成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("修改失败,失败信息为" + e.getMessage(), null); 
        }
	};
	
	/**
     * 分页查看记录
     * @param data 分页参数字符串
     */
    public ResultData findDataTbDsjRuleEditByPage(String data) {
        try {
        	JTbDsjRuleEdit jtbDsjRuleEdit = JSON.parseObject(data, JTbDsjRuleEdit.class); 
        	JSONObject jsonObject  = null;
        	Integer pageIndex = jtbDsjRuleEdit.getPageIndex();
        	Integer pageSize = jtbDsjRuleEdit.getPageSize();
        	
        	if(null == pageIndex || null == pageSize){
        		return ResultData.ResultDataFaultSelf("分页参数没有传", null); 
        	}
        	
        	if(null!=jtbDsjRuleEdit.getCondition()){
    			jsonObject  = JSON.parseObject(jtbDsjRuleEdit.getCondition().toString());
    		}

			String process_rule_description = null;
			if (!StringUtils.isEmpty(jsonObject.get("process_rule_description"))) {
				process_rule_description = jsonObject.get("process_rule_description").toString();
			}

			String calc_result_variable = null;
			if (!StringUtils.isEmpty(jsonObject.get("calc_result_variable"))) {
				calc_result_variable = jsonObject.get("calc_result_variable").toString();
			}
			String dic_variable_name_calc = null;
			if (!StringUtils.isEmpty(jsonObject.get("dic_variable_name_calc"))) {
				dic_variable_name_calc = jsonObject.get("dic_variable_name_calc").toString();
			}


    		List<TbDsjRuleEdit> list = tbDsjRuleEditDao.findDataTbDsjRuleEditByPage((pageIndex-1)*pageSize, pageIndex*pageSize,process_rule_description,calc_result_variable,dic_variable_name_calc);
    		Integer count = tbDsjRuleEditDao.findDataTbDsjRuleEditByPageSize(process_rule_description,calc_result_variable,dic_variable_name_calc);
    		return ResultData.ResultDataSuccess(list, count);
        } catch (Exception e) {
        	e.printStackTrace();
        	return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null); 
        }
    }
    
    /**
     * 根据主键查询单条记录
     * @param data 分页参数字符串
     */
    public ResultData findDataTbDsjRuleEditByIndocno(String data) {
        try {
        	JTbDsjRuleEdit jtbDsjRuleEdit = JSON.parseObject(data, JTbDsjRuleEdit.class); 
        	Long indocno = jtbDsjRuleEdit.getIndocno();
        	
    		TbDsjRuleEdit tbDsjRuleEdit = tbDsjRuleEditDao.findDataTbDsjRuleEditByIndocno(indocno);
    		return ResultData.ResultDataSuccess(tbDsjRuleEdit);
        } catch (Exception e) {
        	e.printStackTrace();
        	return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null); 
        }
    }
	
	/**
	 * 查看记录
	 * @return list 对象集合返回
	 * */
	public List<TbDsjRuleEdit> findDataTbDsjRuleEdit(){
		List<TbDsjRuleEdit> list = tbDsjRuleEditDao.findDataTbDsjRuleEdit();
		return list;
	};

	public ResultData findDataTbDsjRuleEditshi621(){
		String data=restTemplate.getForObject("http://170.0.200.11:8090/restapii?name=C621_zonetrk", String.class);
		JSONObject jsonObject = JSONObject.parseObject(data);
		JSONArray zone_row = jsonObject.getJSONArray("zone_row");
		List<ZoneEntity> list2 = JSONObject.parseArray(zone_row.toString(), ZoneEntity.class);
		return ResultData.ResultDataSuccess(list2, 0);
	}

	public ResultData findDataTbDsjRuleEditshi622(){
		String data=restTemplate.getForObject("http://170.0.200.11:8090/restapii?name=C622_zonetrk", String.class);
		JSONObject jsonObject = JSONObject.parseObject(data);
		JSONArray zone_row = jsonObject.getJSONArray("zone_row");
		List<ZoneEntity> list2 = JSONObject.parseArray(zone_row.toString(), ZoneEntity.class);
		return ResultData.ResultDataSuccess(list2, 0);
	}
	public ResultData findDataTbDsjRuleEditshi642(){
		String data=restTemplate.getForObject("http://170.0.200.11:8090/restapii?name=C642_zonetrk", String.class);
		JSONObject jsonObject = JSONObject.parseObject(data);
		JSONArray zone_row = jsonObject.getJSONArray("zone_row");
		List<ZoneEntity> list2 = JSONObject.parseArray(zone_row.toString(), ZoneEntity.class);
		return ResultData.ResultDataSuccess(list2, 0);
	}



}
